package cn.wolfcode.wolf2w.strategy.listener;

import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
/**
 * spring 事件监听器  实现 ApplicationListener 监听事件通用接口
 * ContextRefreshedEvent 上下文更新事件
 */
@Component
public class RedisStatisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private RedisService redisService;


    /**
     *  mysql 初始化redis
     * @param event
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        String key = RedisKeys.STRATEGY_STATIS_INIT.join("init");

        Boolean b = redisService.hasKey(key);
        //先判断有没有初始化的key
        if (b) {
            //如果有就结束
            System.out.println("redis statisHash数据已经初始化完成");
            return;
        }
        //没有就先加入初始化key,在进行初始化
        redisService.setCacheObject(key,1);
        strategyService.statisHashInit();

        System.out.println("strategy攻略服务初始化");
    }
}
